<?php
class RegisteTeacherClass extends E_teacher {
	public $Message;
}

class TeacherLib {
	private $model;
	private $imageLib;
	function __construct() {
		require_once APP_LIB_PATH . 'imageLib.php';
		require_once APP_MODEL_PATH . 'e_teacher.php';
		$this->imageLib = new ImageLib ();
		$this->model = new TeacherModel ();
	}
	public function checkForm() {
		ViewData::$form_data = BaseAction::parseForm ( "POST", ViewData::$form_data );
		ViewData::$form_hint->TeacherName = FormLib::getName ( 'TeacherName' );
		ViewData::$form_hint->Sex = FormLib::getTinyInt4 ( 'Sex' );
		ViewData::$form_hint->Nation = FormLib::getText64 ( 'Nation' );
		ViewData::$form_hint->Height = FormLib::getInt ( 'Height' );
		ViewData::$form_hint->Weight = FormLib::getInt ( 'Weight' );
		ViewData::$form_hint->Political = FormLib::getText64 ( 'Political' );
		ViewData::$form_hint->BirthdayYear = FormLib::getInt ( 'BirthdayYear' );
		ViewData::$form_hint->BirthdayMonth = FormLib::getInt ( 'BirthdayMonth' );
		ViewData::$form_hint->Origin = FormLib::getText64 ( 'Origin' );
		ViewData::$form_hint->Graduation = FormLib::getText64 ( 'Graduation' );
		ViewData::$form_hint->Education = FormLib::getText64 ( 'Education' );
		ViewData::$form_hint->EducationalSystem = FormLib::getText64 ( 'EducationalSystem' );
		ViewData::$form_hint->Professional = FormLib::getText64 ( 'Professional' );
		ViewData::$form_hint->GraduateSchool = FormLib::getText64 ( 'GraduateSchool' );
		ViewData::$form_hint->Address = FormLib::getText64 ( 'Address' );
		ViewData::$form_hint->EnglishLevel = FormLib::getText64 ( 'EnglishLevel' );
		ViewData::$form_hint->ComputerLevel = FormLib::getText64 ( 'ComputerLevel' );
		ViewData::$form_hint->GoodAt = FormLib::getText64 ( 'GoodAt' );
		ViewData::$form_hint->Phone = FormLib::getText64 ( 'Phone' );
		ViewData::$form_hint->Mobile = FormLib::getMobile ( 'Mobile' );
		ViewData::$form_hint->QQ = FormLib::getQQ ( 'QQ' );
		ViewData::$form_hint->Soft = FormLib::getText64 ( 'Soft' );
		ViewData::$form_hint->WorkTime = FormLib::getText64 ( 'WorkTime' );
		ViewData::$form_hint->WorkSchool = FormLib::getText64 ( 'WorkSchool' );
		ViewData::$form_hint->WorkProfessional = FormLib::getText64 ( 'WorkProfessional' );
		ViewData::$form_hint->Introduction = FormLib::getIntroduction ();
		ViewData::$form_hint->Assessment = FormLib::getAssessment ();
		return;
	}
	
	public function checkEditForm() {
		ViewData::$form_data = BaseAction::parseForm ( "POST", ViewData::$form_data );
		ViewData::$form_hint->TeacherName = FormLib::getName ( 'TeacherName' );
		ViewData::$form_hint->Sex = FormLib::getTinyInt4 ('Sex' );
		ViewData::$form_hint->Nation = FormLib::getNation (); //民族
		ViewData::$form_hint->Height = FormLib::getHeight ();
		ViewData::$form_hint->Weight = FormLib::getWeight ();
		ViewData::$form_hint->Political = FormLib::getPolitical (); //政治面貌 
		ViewData::$form_hint->BirthdayYear = FormLib::getBirthdayYear ();
		ViewData::$form_hint->BirthdayMonth = FormLib::getBirthdayMonth ();
		ViewData::$form_hint->Origin = FormLib::getOrigin (); //籍贯
		ViewData::$form_hint->Graduation = FormLib::getGraduation (); //毕业时间
		ViewData::$form_hint->Education = FormLib::getEducation (); //学历
		ViewData::$form_hint->EducationalSystem = FormLib::getEducationalSystem (); //学制
		ViewData::$form_hint->Professional = FormLib::getProfessional (); //专业
		ViewData::$form_hint->GraduateSchool = FormLib::getGraduateSchool (); //毕业学校
		ViewData::$form_hint->Address = FormLib::getAddress ();
		ViewData::$form_hint->EnglishLevel = FormLib::getEnglishLevel ();
		ViewData::$form_hint->ComputerLevel = FormLib::getComputerLevel ();
		ViewData::$form_hint->Mobile = FormLib::getMobile ();
		ViewData::$form_hint->Introduction = FormLib::getIntroduction (); //自我介绍
		ViewData::$form_data->Photo = $_FILES ['Photo'];
		if ($_FILES ['Photo'] ['name']) {
			ViewData::$form_data->Photo = $_FILES ['Photo'];
			ViewData::$form_hint->Photo = $this->imageLib->filtPhoto ( ViewData::$form_data->Photo );
		}
		return;
	}
	public function registeLogic() {
		$maxId = $this->model->getMaxId ();
		$info->Id = $maxId ['maxId'] + 1;
		
		$info = ViewData::$form_data;
		$registerSession = UserSession::getRegisteInfo ();
		$info->Photo = $_FILES ['Photo'];
		if ($info->Photo ['name']) {
			ViewData::$form_hint->Photo = $this->imageLib->filtPhoto ( $info->Photo );
			if ($this->imageLib->filtPhoto ( $info->Photo ) == TRUE) {
				$info->Photo = $this->imageLib->addPhoto ( $info->Photo, 'teacher', $maxId ['maxId'] + 1 );
			} else {
				$info->Photo == '';
			}
		} else {
			$info->Photo == '';
		}
		$info->RegistTime = date ( 'Y-m-d H:i:s' );
		$info->RegistStatus = '0';
		$teacherId = $this->model->addInfo ( $info );
		if (! $teacherId) {
			throw new Exception ( "您已经在本站注册过，可以直接登录系统" );
		}
		return $teacherId;
	}
	public function editLogic($teacherId) {
		$info = ViewData::$form_data;
		$info->Photo = $_FILES ['Photo'];
		if ($info->Photo ['name']) {
			ViewData::$form_hint->Photo = $this->imageLib->filtPhoto ( $info->Photo );
			if ($this->imageLib->filtPhoto ( $info->Photo ) == TRUE) {
				$info->Photo = $this->imageLib->addPhoto ( $info->Photo, 'teacher', $studentId );
			} else {
				$info->Photo == '';
			}
		} else {
			$info->Photo == '';
		}
		
		if (! $this->model->setInfoById ( $info, $teacherId )) {
			throw new Exception ( "用户信息修改失败" );
		}
		return TRUE;
	}
	
	public function editTeacherStatusLogic($teacherId,$status) {
		$info->RegistStatus=$status;
		if (! $this->model->setInfoById ( $info, $teacherId )) {
			throw new Exception ( "用户信息修改失败" );
		}
		return TRUE;
	}
	
	
	public function getById($teacherId) {
		$info = $this->model->getById ( $teacherId );
		return $info;
	}
	public function setInfoById($info, $teacherId) {
		return $this->model->setInfoById ( $info, $teacherId );
	}
	public function addRegistedNumber($gradeId) {
		$info = $this->gradeModel->getByGradeId ( $gradeId );
		if (! $info) {
			return FALSE;
		}
		if ($info->Number <= $info->RegistedNumber) {
			return FALSE;
		}
		$info->RegistedNumber += 1;
		return $this->gradeModel->setInfoById ( $info, $gradeId );
	}
	
	//==================================管理后台=======================================================
	

	public function checkPaymentForm() {
		ViewData::$form_data = BaseAction::parseForm ( "POST", ViewData::$form_data );
		require_once APP_LIB_PATH . 'formLib.php';
		ViewData::$form_hint->Cost = FormLib::getCost ();
		ViewData::$form_hint->RegistStatus = FormLib::getRegistStatus ();
		return;
	}
	public function editPaymentLogic($teacherId) {
		$info = ViewData::$form_data;
		if ($info->RegistStatus == 2) {
			$model = new E_finance_daily ();
			$model->InMoney = $info->Cost;
			$model->OutMoney = 0;
			if (! $gradeInfo) {
				throw new Exception ( "请不要非法操作" );
			}
			$model->Description = ViewData::$form_data->StudentName . '全日制报名费' . ViewData::$form_data->Cost . '元';
			$model->Balance = ViewData::$form_data->Cost;
			$model->MoneyType = $_GET ['gradeId'];
			require_once APP_LIB_PATH . 'financeLib.php';
			$financeLib = new FinanceLib ();
			$financeLib->addDaily ( $model );
		}
		$data->Cost = $info->Cost;
		$data->RegistStatus = $info->RegistStatus;
		return $this->model->setInfoById ( $data, $studentId );
	}
	
	public function getTeacherList() {
		return $this->model->getList ();
	
	}
	public function delByTeacherId($teacherId) {
		return $this->model->delByTeacherId ( $teacherId );
	}
	
	public function exportExcel($idList) {
		$info = array ();
		$info [0] = array ('姓名', '性别', '民族', '出生年月', '手机号码', 'QQ', '身高', '体重', '政治面貌', '籍贯', '毕业时间', '学历', '学制','专业', '毕业学校', '家庭地址', '英语水平', '计算机水平' );
		foreach ( $idList as $value ) {
			$studentInfo = $this->model->getById ( $value );
			$info [$value] ['TeacherName'] = $studentInfo->TeacherName;
			if ($studentInfo->Sex == 1) {
				$info [$value] ['Sex'] = "男";
			} elseif ($studentInfo->Sex == 2) {
				$info [$value] ['Sex'] = "女";
			}
			
			$info [$value] ['Nation'] = $studentInfo->Nation;
			$info [$value] ['Birthday'] = $studentInfo->BirthdayYear.'年' . $studentInfo->BirthdayMonth . '月';
			$info [$value] ['Mobile'] = $studentInfo->Mobile;
			$info [$value] ['QQ'] = $studentInfo->QQ;
			
			$info [$value] ['Height'] = $studentInfo->Height;
			$info [$value] ['Weight'] = $studentInfo->Weight;
			$info [$value] ['Political'] = $studentInfo->Political; //政治面貌 
			
			$info [$value] ['Origin'] = $studentInfo->Origin; //籍贯
			$info [$value] ['Graduation'] = $studentInfo->Graduation; //毕业时间
			$info [$value] ['Education'] = $studentInfo->Education; //学历
			$info [$value] ['EducationalSystem'] = $studentInfo->EducationalSystem; //学制
			$info [$value] ['Professional'] = $studentInfo->Professional; //专业
			$info [$value] ['GraduateSchool'] = $studentInfo->GraduateSchool; //毕业学校
			$info [$value] ['Address'] = $studentInfo->Address;
			$info [$value] ['EnglishLevel'] = $studentInfo->EnglishLevel;
			$info [$value] ['ComputerLevel'] = $studentInfo->ComputerLevel;
		}
		require SOURCE_EXT_PATH . 'library/ExportExcelLib.php';
		$xls = new ExportExcelLib ( 'UTF-8', false, 'My Test Sheet' );
		$xls->addArray ( $info );
		$xls->generateXML ( 'teacher' );
		exit ();
	}
}